package cn.edu.scau.cmi.ema.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.Session;


import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;


import cn.edu.scau.cmi.ema.util.CmiSqlUtil;

@Repository("TensorflowDAO")
@Transactional
public class TensorflowDAOImpl extends AbstractJpaDao<Tensorflow> implements TensorflowDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Tensorflow.class }));

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	public TensorflowDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Tensorflow findTensorflowById(Integer id) throws DataAccessException {

		return findTensorflowById(id, -1, -1);
	}

	@Transactional
	public Tensorflow findTensorflowById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findTensorflowById", startResult, maxRows, id);
			return (Tensorflow) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Tensorflow> findTensorflowByTitle(String title) throws DataAccessException {

		return findTensorflowByTitle(title, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Tensorflow> findTensorflowByTitle(String title, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findTensorflowByTitle", startResult, maxRows, title);
		return new LinkedHashSet<Tensorflow>(query.getResultList());
	}

	@Transactional
	public Set<Tensorflow> findTensorflowByTitleContaining(String title) throws DataAccessException {

		return findTensorflowByTitleContaining(title, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Tensorflow> findTensorflowByTitleContaining(String title, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findTensorflowByTitleContaining", startResult, maxRows, title);
		return new LinkedHashSet<Tensorflow>(query.getResultList());
	}

	@Transactional
	public Set<Tensorflow> findTensorflowByHref(String href) throws DataAccessException {

		return findTensorflowByHref(href, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Tensorflow> findTensorflowByHref(String href, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findTensorflowByHref", startResult, maxRows, href);
		return new LinkedHashSet<Tensorflow>(query.getResultList());
	}

	@Transactional
	public Set<Tensorflow> findTensorflowByHrefContaining(String href) throws DataAccessException {

		return findTensorflowByHrefContaining(href, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Tensorflow> findTensorflowByHrefContaining(String href, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findTensorflowByHrefContaining", startResult, maxRows, href);
		return new LinkedHashSet<Tensorflow>(query.getResultList());
	}

	@Transactional
	public Set<Tensorflow> findTensorflowByTime(String time) throws DataAccessException {

		return findTensorflowByTime(time, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Tensorflow> findTensorflowByTime(String time, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findTensorflowByTime", startResult, maxRows, time);
		return new LinkedHashSet<Tensorflow>(query.getResultList());
	}

	@Transactional
	public Set<Tensorflow> findTensorflowByTimeContaining(String time) throws DataAccessException {

		return findTensorflowByTimeContaining(time, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Tensorflow> findTensorflowByTimeContaining(String time, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findTensorflowByTimeContaining", startResult, maxRows, time);
		return new LinkedHashSet<Tensorflow>(query.getResultList());
	}

	@Transactional
	public Set<Tensorflow> findTensorflowByAuthor(String author) throws DataAccessException {

		return findTensorflowByAuthor(author, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Tensorflow> findTensorflowByAuthor(String author, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findTensorflowByAuthor", startResult, maxRows, author);
		return new LinkedHashSet<Tensorflow>(query.getResultList());
	}

	@Transactional
	public Set<Tensorflow> findTensorflowByAuthorContaining(String author) throws DataAccessException {

		return findTensorflowByAuthorContaining(author, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Tensorflow> findTensorflowByAuthorContaining(String author, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findTensorflowByAuthorContaining", startResult, maxRows, author);
		return new LinkedHashSet<Tensorflow>(query.getResultList());
	}

	@Transactional
	public Set<Tensorflow> findTensorflowBySource(String source) throws DataAccessException {

		return findTensorflowBySource(source, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Tensorflow> findTensorflowBySource(String source, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findTensorflowBySource", startResult, maxRows, source);
		return new LinkedHashSet<Tensorflow>(query.getResultList());
	}

	@Transactional
	public Set<Tensorflow> findTensorflowBySourceContaining(String source) throws DataAccessException {

		return findTensorflowBySourceContaining(source, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Tensorflow> findTensorflowBySourceContaining(String source, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findTensorflowBySourceContaining", startResult, maxRows, source);
		return new LinkedHashSet<Tensorflow>(query.getResultList());
	}

	@Transactional
	public Set<Tensorflow> findTensorflowByContent(String content) throws DataAccessException {

		return findTensorflowByContent(content, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Tensorflow> findTensorflowByContent(String content, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findTensorflowByContent", startResult, maxRows, content);
		return new LinkedHashSet<Tensorflow>(query.getResultList());
	}

	@Transactional
	public Set<Tensorflow> findTensorflowByContentContaining(String content) throws DataAccessException {

		return findTensorflowByContentContaining(content, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Tensorflow> findTensorflowByContentContaining(String content, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findTensorflowByContentContaining", startResult, maxRows, content);
		return new LinkedHashSet<Tensorflow>(query.getResultList());
	}

	@Transactional
	public Set<Tensorflow> findTensorflowByTokenization(String tokenization) throws DataAccessException {

		return findTensorflowByTokenization(tokenization, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Tensorflow> findTensorflowByTokenization(String tokenization, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findTensorflowByTokenization", startResult, maxRows, tokenization);
		return new LinkedHashSet<Tensorflow>(query.getResultList());
	}

	@Transactional
	public Set<Tensorflow> findTensorflowByTokenizationContaining(String tokenization) throws DataAccessException {

		return findTensorflowByTokenizationContaining(tokenization, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Tensorflow> findTensorflowByTokenizationContaining(String tokenization, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findTensorflowByTokenizationContaining", startResult, maxRows, tokenization);
		return new LinkedHashSet<Tensorflow>(query.getResultList());
	}

	@Transactional
	public Set<Tensorflow> findTensorflowByIs_ema(Boolean is_ema) throws DataAccessException {

		return findTensorflowByIs_ema(is_ema, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Tensorflow> findTensorflowByIs_ema(Boolean is_ema, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findTensorflowByIs_ema", startResult, maxRows, is_ema);
		return new LinkedHashSet<Tensorflow>(query.getResultList());
	}

	@Transactional
	public Set<Tensorflow> findTensorflowByName(String name) throws DataAccessException {

		return findTensorflowByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Tensorflow> findTensorflowByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findTensorflowByName", startResult, maxRows, name);
		return new LinkedHashSet<Tensorflow>(query.getResultList());
	}

	@Transactional
	public Set<Tensorflow> findTensorflowByNameContaining(String name) throws DataAccessException {

		return findTensorflowByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Tensorflow> findTensorflowByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findTensorflowByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Tensorflow>(query.getResultList());
	}

	@Transactional
	public Set<Tensorflow> findTensorflowByDescription(String description) throws DataAccessException {

		return findTensorflowByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Tensorflow> findTensorflowByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findTensorflowByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Tensorflow>(query.getResultList());
	}

	@Transactional
	public Set<Tensorflow> findTensorflowByDescriptionContaining(String description) throws DataAccessException {

		return findTensorflowByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Tensorflow> findTensorflowByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findTensorflowByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Tensorflow>(query.getResultList());
	}

	@Transactional
	public Tensorflow findTensorflowByPrimaryKey(Integer id) throws DataAccessException {
		return findTensorflowByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Tensorflow findTensorflowByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findTensorflowByPrimaryKey", startResult, maxRows, id);
			return (Tensorflow) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Tensorflow> findTensorflowsByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findTensorflowsByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Tensorflow> findAllTensorflows() throws DataAccessException {

		return findAllTensorflows(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Tensorflow> findAllTensorflows(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllTensorflows", startResult, maxRows);
		return new LinkedHashSet<Tensorflow>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Tensorflow> findTensorflowByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Tensorflow>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Tensorflow> findTensorflowsByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Tensorflow.class);
		Session session = entityManager.unwrap(Session.class);
		Set<Tensorflow> tensorflows = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return tensorflows;
	}

	@Transactional
	public Tensorflow findTensorflowByPhysicalPrimaryKey(String name) throws DataAccessException {
		return findTensorflowByPhysicalPrimaryKey(name, -1, -1);
	}

	@Transactional
	public Tensorflow findTensorflowByPhysicalPrimaryKey(String name, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findTensorflowByPhyscialPrimaryKey", startResult, maxRows, name);
			return (Tensorflow) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Tensorflow entity) {
		return true;
	}
}
